<template>
  <!-- 底部 -->
  <div class="my-footer">
    <!-- 全选 -->
    <div class="custom-control custom-checkbox">
      <input type="checkbox" v-model="isAll" class="custom-control-input" id="footerCheck">
      <label class="custom-control-label" for="footerCheck">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计:</span>
      <span class="price">¥ {{selPrice}}</span>
    </div>
    <!-- 按钮 -->
    <button type="button" class="footer-btn btn btn-primary">结算 ({{selected}})</button>
  </div>
</template>

<script>
export default {
  props: {
    cartList: {
      type: Array,
      default: () => ({})
    }
  },
  computed: {
    // 全选功能
    isAll: {
      get () {
        return this.cartList.every(item => item.goods_state)
      },
      set (newVal) {
        this.cartList.forEach(item => {
          item.goods_state = newVal
        })
      }
    },
    // 计算数量总和
    selected () {
      const selted = this.cartList.filter(item => item.goods_state)
      return selted.reduce((acc, item) => {
        // eslint-disable-next-line no-return-assign
        return acc += item.goods_count
      }, 0)
    },
    // 计算结算金额总和
    selPrice () {
      const selted = this.cartList.filter(item => item.goods_state)
      return selted.reduce((acc, item) => {
        const all = item.goods_count * item.goods_price
        // eslint-disable-next-line no-return-assign
        return acc += all
      }, 0)
    }
  }
}
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: #fff;
  #footerCheck {
    margin-right: 5px;
  }
  .price {
    color: red;
    font-weight: bold;
    font-size: 15px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>
